<?php
// @filename   common_helper.php
// @version    1.0
// @author     洪明伟@shstorm
// @contact    hongmingwei@shstorm.com
// @update     2009-10-16
// @comment    Some Comments

if(!function_exists('get_discount')) {
    function get_discount($username,$orderid) {
        $CI = & get_instance();
        $CI->load->model('Morders');
        $oinfo = $CI->Morders->get_order_info($orderid);
        $CI->load->model('Mproduct');
        $pinfo = $CI->Mproduct->get_product_info($oinfo['product']);
        $CI->load->model('Mmember');
        $uinfo = $CI->Mmember->get_user_info('username',$username);
        if(empty($uinfo) || empty($pinfo)){
            return '0';
        }
        //print_r($uinfo);
        //print_r($pinfo);
        //print_r($oinfo);
        if(strtoupper($oinfo['uid']) == strtoupper($username)){
            $discount = round($oinfo['price'] * ($uinfo['discount'] /100),2);
        } elseif (strtoupper($oinfo['dai']) == strtoupper($username)){
            if(!$oinfo['uid'] || substr(strtoupper($oinfo['uid']),0,2) != 'WB'){
                $discount = round(($oinfo['price'] + ($pinfo['week_num']*$pinfo['week_money']))*$pinfo['wb_discount']/100,2);
            } else {
                $discount = '0';
            }
        }
        /**
        if(strtoupper($uinfo['utype']) == 'WB') {
            $discount = round(($oinfo['price'] + ($pinfo['week_num']*$pinfo['week_money']))*$pinfo['wb_discount']/100,2);
        } else {
            $discount = round($oinfo['price'] * ($uinfo['discount'] /100),2);
        }
        **/
        //echo $discount;exit;
        return $discount;
    }
}


if(!function_exists('get_order_info')) {
    function get_order_info($order_id) {
        $CI = & get_instance();
        $CI->load->model('Morders');
        $info = $CI->Morders->get_order_info($order_id);
        //print_r($info);exit;
        $info['order_discount'] = get_order_discount($order_id);
        $CI->load->model('Mproduct');
        $p_info = $CI->Mproduct->get_product_info($info['product']);
        if(!empty($p_info)){
            $info['p_name'] = $p_info['p_name'];
            $info['p_days'] = $p_info['p_days'];
            $info['week_num'] = $p_info['week_num'];
            $info['week_money'] = $p_info['week_money'];
        } else {
            $info['p_name'] = '';
            $info['p_days'] = '';
            $info['week_num'] = 0;
            $info['week_money'] = 0;
        }
        $info['order_discount'] = get_discount($info['uid'],$order_id);
        $state = get_order_state($order_id,1);
        $info['state'] = key($state);
        $info['state_desc'] = $state[$info['state']];
        $CI->load->model('Mgames');
        $info['game_name']      = $CI->Mgames->get_game_name($info['game_id']);
        $info['area_name']      = $CI->Mgames->get_area_name($info['game_area']);
        $info['server_name']    = $CI->Mgames->get_server_name($info['game_server']);
        $info['group_name']     = $CI->Mgames->get_group_info('en_name',$info['role_group'],'cn_name');
        $info['career_name']    = $CI->Mgames->get_career_info('en_name',$info['role_career'],'cn_name');
        $CI->load->model('Mimportant');
        $iinfo = $CI->Mimportant->get_important_by_orderid($order_id);
        if(!empty($iinfo)){
        	$info['iid'] = $iinfo['id'];
        	$info['account'] = $iinfo['account'];
        	$info['wb_name'] = $iinfo['wb_name'];
        	$info['pwd'] = $iinfo['pwd'];
        	$info['wb_dai'] = $iinfo['wb_dai'];
        	$info['img'] = $iinfo['img'];
        }
        //print_r($info);
        return $info;
    }
}

if(!function_exists('get_member_order_info')) {
    function get_member_order_info($order_id,$dai=0) {
        $CI = & get_instance();
        $CI->load->model('Morders');
        $info = $CI->Morders->get_order_info($order_id);
        $info['order_discount'] = get_order_discount($order_id);
        $CI->load->model('Mproduct');
        $p_info = $CI->Mproduct->get_product_info($info['product']);
        if(!empty($p_info)){
            $info['p_name'] = $p_info['p_name'];
            $info['p_days'] = $p_info['p_days'];
            $info['week_num'] = $p_info['week_num'];
            $info['week_money'] = $p_info['week_money'];
        } else {
            $info['p_name'] = '';
            $info['p_days'] = '';
            $info['week_num'] = 0;
            $info['week_money'] = 0;
        }
        $info['order_discount'] = get_discount($info['uid'],$order_id);
        $state = $dai == 0 ? get_order_state($order_id) : get_order_state($order_id,0,1);
        $info['state'] = key($state);
        $info['state_desc'] = $state[$info['state']];
        //print_r($info);
        $CI->load->model('Mgames');
        $info['game_name']      = $CI->Mgames->get_game_name($info['game_id']);
        $info['area_name']      = $CI->Mgames->get_area_name($info['game_area']);
        $info['server_name']    = $CI->Mgames->get_server_name($info['game_server']);
        $info['group_name']     = $CI->Mgames->get_group_info('en_name',$info['role_group'],'cn_name');
        $info['career_name']    = $CI->Mgames->get_career_info('en_name',$info['role_career'],'cn_name');
        return $info;
    }
}

if(!function_exists('get_order_state')){
	function get_order_state($orderid,$guest=0,$dai=0){
		$CI = & get_instance();
        $CI->load->model('Morders');
        $info = $CI->Morders->get_order_info($orderid);
        $CI->config->load('setup');
        $gt_states = $CI->config->item('guest_state');
        $cm_states = $CI->config->item('cm_state');
        $wb_states = $CI->config->item('wb_state');
        $state = '0';
        $desc = '其他';
        if($guest){
        	$state = $info['dai_state'] ? $info['dai_state'] : $info['order_state'];
        	$desc = $gt_states[$state];
        } else {
	        $utype = substr(strtoupper($info['uid']),0,2);
	        if($utype == 'CM' || $utype == 'NB'){
	        	$state = $info['order_state'];
	        	$desc = $cm_states[$state];
	        } elseif ($utype == 'WB'){
	        	if($info['order_state'] <= 3){
	        		$state = $info['order_state'];
	        	} elseif ($info['order_state'] > 4){
	        		$state = $info['dai_state'] ? $info['dai_state'] : $info['order_state'];
	        	}
	        	$desc = $wb_states[$state];
	        }
        }
        if($dai){
        	$state = $info['dai_state'] ? $info['dai_state'] : $info['order_state'];
        	$desc = $wb_states[$state];
        }
        return array($state=>$desc);
	}
}


if(!function_exists('get_order_discount')){
    function get_order_discount($order_id){
        $CI = & get_instance();
    	$CI->load->model('Morders');
    	$info = $CI->Morders->get_order_discount($order_id);
    	return $info ? $info['discount'] : '0.00';
    }
}

function get_config_value($key) {
    $CI = & get_instance();
    $CI->load->model('Mconfigs');
    return $CI->Mconfigs->get_config_value($key);
}

?>